library(SDMTools)
base.dir='/home/jc165798/Climate/CIAS/global.worldclim/'
image.dir='/home/jc148322/tmp/'
base.asc = read.asc('/home/jc165798/Climate/CIAS/global.worldclim/10_arcminute/baseline.50to00/bioclim/bioclim_01.asc') #read in the base ascii grid file


vois=c('10_arcminute', '5_arcminute')


cols = colorRampPalette(c("#A50026","#D73027","#F46D43","#FDAE61","#FEE090","#FFFFBF","#E0F3F8","#ABD9E9","#74ADD1","#4575B4","#313695"))(21)
rain.cols=colorRampPalette(c("#FEE090","#FFFFBF","#E0F3F8","#ABD9E9","#74ADD1","#4575B4","#313695"))(21)
cols=cols[21:1]
pnts=cbind(x=c(-160,-170,-170,-160), y=c(-25,-25,-50,-50))  


for (voi in vois) { cat(voi, '\n')

	png(paste(image.dir, voi,'.png',sep=''),width=dim(base.asc)[1]+30, height=dim(base.asc)[2]*2+60, units='px', pointsize=20, bg='lightgrey')
	par(mar=c(0,2,2,0),mfrow=c(2,1),cex=1,oma=c(3,3,3,0))

	sub.dir=paste(base.dir, voi, '/baseline.50to00/bioclim/',sep='')
	
	bio1=read.asc(paste(sub.dir,'bioclim_01.asc',sep=''))
	zlim=range(bio1,na.rm=T)
	image(bio1, zlim=zlim, ann=FALSE,axes=FALSE,col=cols)
	mtext('bioclim_01', line=1,  side=2, cex=2)
	mtext(voi, line=1,  side=3, cex=2)
	legend.gradient(pnts,cols=cols,limits=round(zlim,digits=2), title='Mean Temp', cex=2)
	
	bio12=read.asc(paste(sub.dir,'bioclim_12.asc',sep=''))
	zlim=range(bio12,na.rm=T)
	image(bio12, zlim=zlim, ann=FALSE,axes=FALSE,col=rain.cols)
	mtext('bioclim_12', line=1,  side=2, cex=2)
	legend.gradient(pnts,cols=rain.cols,limits=round(zlim,digits=2), title='Precip', cex=2)
	dev.off()
}

